package com.chwn.curiousdiary.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.R;

/**
 * Created by Myshikin on 2017/4/10.
 */

public class DialogUtil {

    private static Context  mContext;
    private static Dialog   mDialog;
    private static TextView mDialogTV;

    public static Context getContext() {
        mContext = DiaryApplication.mInstance;
        return mContext;
    }

    /**
     * @param context
     * @param view
     * @param x
     *         显示的x坐标
     * @param y
     *         显示的y坐标
     * @param width
     *         dialog的宽度
     * @param height
     *         dialog的高度
     * @param animation
     *         显示和消失的动画效果
     * @param cancleAble
     *         是否可以cancle掉
     * @param isAnimation
     * @return
     */
    private static Dialog showDialog(Context context, View view, int x, int y, int width, int height, int animation, boolean cancleAble, boolean isAnimation) {
        Dialog ad;
        if (Build.VERSION.SDK_INT >= 11) {
            ad = new AlertDialog.Builder(context, R.style.alert_dialog).create();
        } else {
            ad = new Dialog(context, R.style.alert_dialog);
        }
        ad.setCancelable(cancleAble);
        WindowManager.LayoutParams lp = ad.getWindow().getAttributes();
        lp.x = x;
        lp.y = y;
        ad.show();
        ad.setContentView(view);
        ad.getWindow().setLayout(width, height);
    /*if (isAnimation) {
            if (animation != 0)
				ad.getWindow().setWindowAnimations(animation);
			else
				ad.getWindow().setWindowAnimations(R.style.head_in_out);
		}*/
        ad.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        return ad;
    }

    /**
     * 弹出提示用户等待的dialog
     */
    public static void showWaitDialog(Context context, String str, boolean canCancle) {
        if (mDialog == null || !mDialog.isShowing()) {
            View vv = View.inflate(context, R.layout.dialog_wait, null);
            mDialogTV = (TextView) vv.findViewById(R.id.tv_dialog);
            if (!TextUtils.isEmpty(str)) {
                mDialogTV.setText(str);
            }
            mDialog = showDialog(context, vv, 0, 0, (int) (getsx() / 1.8), (int) (getsy() / 6.5), 0, canCancle, false);
        } else {
            if (!TextUtils.isEmpty(str)) {
                mDialogTV.setText(str);
            }
            mDialog.setCancelable(canCancle);
        }
    }

    /**
     * 取消等待的dialog
     */
    public static void dismissWaitDialog() {
        if (mDialog != null)
            mDialog.dismiss();

        mContext = null;
        mDialogTV = null;
        mDialog = null;
    }

    public static int getsx() {
        return getContext().getResources().getDisplayMetrics().widthPixels;
    }

    public static int getsy() {
        int px = Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
        return getContext().getResources().getDisplayMetrics().heightPixels - px;
    }

}
